package com.ycfmedical.bed.present;

import com.ycfmedical.bed.ApiConfigs;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.base.BaseHealthFragment;
import com.ycfmedical.bed.model.HealthResults;
import com.ycfmedical.bed.net.Api;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Presenter决定 View 行为的纯Java类（不访问任何 Android API） Created by Egoist on 2017/2/9.
 */

public class HealthPresenter extends XPresent<BaseHealthFragment>
{

    // 获取健康头条的数据
    public void loadData(final int page, int type)
    {

        Api.getAvatarService()
            .getHealthData(ApiConfigs.NET_KEY, page, Constants.PAGE_SIZE_MUL, type)
                .compose(XApi.<HealthResults> getApiTransformer())// 统一异常处理
                .compose(XApi.<HealthResults> getScheduler())// 绑定线程
                .compose(getV().<HealthResults> bindToLifecycle())// 绑定生命周期，防止程序崩溃
            .subscribe(new ApiSubcriber<HealthResults>()
            {
                @Override
                protected void onFail(NetError error)
                {
                    getV().showError(error);
                }

                @Override
                public void onNext(HealthResults healthResults)
                {
                    getV().showData(page, healthResults);
                }
            });
    }

}
